package com.ibt.dao;


	import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.ibt.entity.Appraisal;
import com.ibt.util.ApplicationContext;

	

	public class AppraisalDAO {

		static Transaction tx;

		public static int insert(Appraisal b) throws Exception {

			Session session = startOperations();
			session.saveOrUpdate(b);
			session.getTransaction().commit();
			endOperations(session);
			return 0;
		}

		public static int update(Appraisal b) throws Exception {
			Session session = startOperations();
			Query qry = session
					.createQuery("update Appraisal set current_points=:points where business_id=:busid and customer_id=:custid");
			//qry.setInteger("points", b.getCurrentPoints());
			////qry.setInteger("busid", b.getBusiness().getBusinessId());
			//qry.setInteger("custid", b.getCustomer().getCustomerId());
			Object ob = qry.executeUpdate();
			session.getTransaction().commit();
			endOperations(session);
			return 0;
		}

		public static int delete(Appraisal b) throws Exception {
			Session session = startOperations();
			session.delete(b);
			// tx.commit();
			endOperations(session);
			return 0;
		}

		private static void endOperations(Session session) {
			session.close();
			session = null;
		}

		private static Session startOperations() throws Exception {
			Session session = ApplicationContext.getHiberSession().openSession();
			tx = session.beginTransaction();
			return session;
		}

		public static List<Appraisal> findAll() throws Exception {
			List<Appraisal> custObjs = new ArrayList<Appraisal>();
			List objs = null;
			Session session = startOperations();
			Query query = session.createQuery("from Appraisal");
			objs = query.list();
			for (Object o : objs) {
				custObjs.add((Appraisal) o);
			}
			endOperations(session);
			return custObjs;
		}

		public static Appraisal findById(int i) throws Exception {
			Session session = startOperations();
			Appraisal c = (Appraisal) session.load(
					Appraisal.class, i);
			endOperations(session);
			return c;
		}

		public static Object getByCustBusId(int id, int i) throws Exception {
			Session session = startOperations();
			System.out.println("id in dao " + id);
			Query qry = session
					.createQuery("from Appraisal where customer_id=:id and business_id=:bid");
			qry.setInteger("id", id);
			qry.setInteger("bid", i);
			Object obj = qry.uniqueResult();
			System.out.println("obj is " + obj);

			endOperations(session);
			return obj;

		}

		public static List<Appraisal> getBusinessCustMap(int userid)
				throws Exception {
			Session session = startOperations();
			System.out.println("id in dao " + userid);
			Query qry = session
					.createQuery("from Appraisal where customer_id=:userid");
			qry.setInteger("userid", userid);
			List<Object> obj = qry.list();
			ArrayList<Appraisal> lst = new ArrayList<Appraisal>();
			for (Object o : obj) {
				System.out.println("obj is " + obj);
				lst.add((Appraisal) o);
			}
			endOperations(session);
			return lst;

		}

		public static List<Appraisal> findByBusId(int bsid) throws Exception {

			Session session = startOperations();
			Query qry = session
					.createQuery("from Appraisal where business_id=:id");
			qry.setInteger("id", bsid);
			List<Appraisal> listBC = qry.list();
			endOperations(session);
			return listBC;
		}

		
	}

	


